﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop; // WindowInteropHelper
using System.Runtime.InteropServices; // DllImportAttribute

namespace VistaShades
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Shade : Window
    {
        private int opacity = 20;

        //------------------------------------------------------------------
        //Allows mouse clicks to be transparent to this window
        public const int WS_EX_TRANSPARENT = 0x00000020;
        public const int GWL_EXSTYLE = (-20);

        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr hwnd, int index);

        [DllImport("user32.dll")]
        public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            // Get this window's handle
            IntPtr hwnd = new WindowInteropHelper(this).Handle;

            // Change the extended window style to include WS_EX_TRANSPARENT
            int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
            SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
        }
        //------------------------------------------------------------------
        
        public Shade()
        {
            InitializeComponent();   
         
            //set initial shade opacity from user settings
            this.opacity = Properties.Settings1.Default.opacity;
            this.shadeGrid.Opacity = this.opacity * 0.01;
            this.shadeGrid.InvalidateVisual();
        }

        public int ShadeOpacity {
            get { return opacity; }
            set { 
                opacity = value;
                this.shadeGrid.Opacity = value * 0.01;
                Properties.Settings1.Default.opacity = value;
                Properties.Settings1.Default.Save();
            }
        }

    }
}
